import uchicago.src.sim.space.Object2DGrid;

/**
 * Class that implements the simulation space of the rabbits grass simulation.
 * @author 
 */

public class RabbitsGrassSimulationSpace {

	private Object2DGrid space;
	private Object2DGrid agentSpace;

	public RabbitsGrassSimulationSpace(int xSize, int ySize){
		space = new Object2DGrid(xSize, ySize);		    
		agentSpace = new Object2DGrid(xSize, ySize);

		for(int i = 0; i < xSize; i++){
			for(int j = 0; j < ySize; j++){
				space.putObjectAt(i,j,new Integer(0));
			}
		}
	}

	public void createField(int xSize, int ySize) {
		for(int x = 0; x < xSize; x++){
			for(int y = 0; y < ySize; y++){
				space.putObjectAt(x, y,new Integer(0));
			}   		
		}
	}

	public int getGrassValue(int x, int y){
		int i;
		if(space.getObjectAt(x,y)!= null){
			i = ((Integer)space.getObjectAt(x,y)).intValue();
		}
		else{
			i = 0;
		}
		return i;
	}

	public int eatGrass(int x, int y){
		int grass = getGrassValue(x, y);
		space.putObjectAt(x, y, new Integer(0));
		return grass;
	}

	public void setGrassAt(int x, int y, int value)
	{
		space.putObjectAt(x, y, new Integer(value));
	}

	public Object2DGrid getCurrentSpace(){
		return space;
	}

	public Object2DGrid getCurrentAgentSpace(){
		return agentSpace;
	}

	public boolean anyRabbit(int x, int y){//check if the cell is already ocupied
		boolean retVal = false;
		if(agentSpace.getObjectAt(x, y)!=null) retVal = true;
		return retVal;
	}


	public boolean addAgent(RabbitsGrassSimulationAgent agent){
		boolean retVal = false;
		int count = 0;
		int countLimit = 10 * agentSpace.getSizeX() * agentSpace.getSizeY();

		while((retVal==false) && (count < countLimit)){
			int x = (int)(Math.random()*(agentSpace.getSizeX()));
			int y = (int)(Math.random()*(agentSpace.getSizeY()));
			if(anyRabbit(x,y) == false){
				agentSpace.putObjectAt(x,y,agent);
				agent.setXY(x,y);
				agent.setRabbitsGrassSimulationSpace(this);
				retVal = true;
			}
			count++;
		}

		if(count==countLimit)
			System.out.println("CANNOT FIND A PLACE TO BORN!!!!!!");

		return retVal;
	}


	public void spreadGrass(int grass_amount){
		// Randomly place grass in grass space
		for(int i = 0; i < grass_amount; i++){

			// Choose coordinates
			int x = (int)(Math.random()*(space.getSizeX()));
			int y = (int)(Math.random()*(space.getSizeY()));

			// Get the value of the object at those coordinates
			int currentValue = getGrassValue(x, y);
			// Replace the Integer object with another one with the new value
			if(currentValue<14)
				space.putObjectAt(x,y,new Integer(currentValue + 1));
		}
	}

	public void removeRabbitAt(int x, int y){
		agentSpace.putObjectAt(x, y, null);
	}

	public boolean moveRabbitAt(int x, int y, int newX, int newY) {
		boolean retVal = false;
		if(!anyRabbit(newX, newY)){
			RabbitsGrassSimulationAgent rgsa = (RabbitsGrassSimulationAgent)agentSpace.getObjectAt(x, y);
			removeRabbitAt(x,y);
			rgsa.setXY(newX, newY);
			agentSpace.putObjectAt(newX, newY, rgsa);
			retVal = true;
		}
		return retVal;
	}

	public int getTotalGrass()
	{
		int totalGrass =0;
		for(int i = 0; i < agentSpace.getSizeX(); i++){
			for(int j = 0; j < agentSpace.getSizeY(); j++){
				totalGrass += getGrassValue(i,j);
			}
		}
		return totalGrass;
	}
}
